package com.sitech.portal.system.dao.ibatis;

import com.sitech.ibnms.core.BaseIbnmsDao;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.portal.system.dao.ISysModuleDao;
import com.sitech.portal.system.domain.TblSYS_MODULE;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysModuleDaoImpl extends BaseIbnmsDao implements ISysModuleDao {

	public void add(TblSYS_MODULE tblSYS_MODULE) {
        this.insert("insertForTblSYS_MODULE", tblSYS_MODULE);
    }

    public void update(TblSYS_MODULE tblSYS_MODULE) {
        this.update("updateForTblSYS_MODULE", tblSYS_MODULE);
    }

    @Override
    public void update(String directory, String newDirectory) {
        HashMap map = new HashMap();
        map.put("MODULE_DIRECTORY", directory);
        map.put("NEW_MODULE_DIRECTORY", newDirectory);
        this.update("updateChildForTblSYS_MODULE", map);
    }

    public void deleteByRoleId(String roleId) {
        this.delete("deleteForTblSYS_MODULE", roleId);
    }

    public void deleteByPKs(String mId) {
        this.delete("deleteForTblSYS_MODULEByPKS", mId);
    }

    public List findList() {
        return this.query("findListForTblSYS_MODULE", null);
    }

    public IPage findListByPK(HashMap map, PageForm pageForm) {
//        String mySql = "";
//        mySql = "  select MODULE_ID,MODULE_NAME,MODULE_PATH,MODULE_ENABLE,IS_REFRESH,IS_VIEW,MODULE_LAYOUT,OPEN_MODULE_ID,VIEW_ORDER "
//                + " from TB_SYS_MODULE  where PARENT_ID='"+tblSYS_MODULE.getPARENT_ID()+"'";
//        if (tblSYS_MODULE.getMODULE_NAME() != null) {
//        	mySql += " and MODULE_NAME like '%"
//                    + tblSYS_MODULE.getMODULE_NAME() + "%'";
//        }
//        mySql += " ORDER BY VIEW_ORDER ";
//        return this.findPage("findListForTblSYS_MODULEByPK", mySql,pageForm);
    	return this.findPage("findModuleListByParentId", map, pageForm);

    }
    
    public IPage findListByPKLoginId(HashMap map, PageForm pageForm) {
	  	return this.findPage("findModuleListByLoginIDAndParentId", map, pageForm);
    }

    public TblSYS_MODULE findObjByPK(String mId) {
        return (TblSYS_MODULE) this.querySingle(
                    "findObjForTblSYS_MODULEByPK", mId);
    }

    public List findlModuleListForTree(String ROLE_ID) {
        if(ROLE_ID==null){
            ROLE_ID = Integer.MAX_VALUE+"";
        }
        return this.query("findlModuleListForTree",ROLE_ID);
    }

    public List findFirstLevelModuleListByLoginID(String LOGIN_ID) {
        return this.query("findFirstLevelModuleListByLoginID", LOGIN_ID);
    }

    public List findFirstLevelModuleListByLoginIDAndSystemId(String LOGIN_ID,
            String SYSTEM_ID) {
        HashMap map = new HashMap();
        map.put("LOGIN_ID", LOGIN_ID);
        map.put("SYSTEM_ID", SYSTEM_ID);
        return this.query(
                    "findFirstLevelModuleListByLoginIDAndSystemId", map);
    }

    /**
     * 根据父节点或者前缀查找下一级module
     * @param LOGIN_ID
     * @param parent_id
     * @return
     */
    public List<HashMap> findModuleListByLoginIDAndParentId(String LOGIN_ID, String parent_id) {
        HashMap map = new HashMap();
        map.put("LOGIN_ID", LOGIN_ID);
        map.put("PARENT_ID", parent_id);
//        //截掉末尾的基数个零
//        String MODULE_ID_PRE = parent_id.replaceAll("0(00)*$", "");
//        map.put("MODULE_ID_PRE", MODULE_ID_PRE);
        return this.query("findModuleListByLoginIDAndParentId", map);
    }

    public List<Map> recursiveQuerySubModules(String loginId, String parentId) {
        HashMap map = new HashMap();
        map.put("LOGIN_ID", loginId);
        map.put("PARENT_ID", parentId);
        return this.query("recursiveQuerySubModules", map);
    }

    /**
     * 根据父节点或者前缀查找下一级module
     * @param parent_id
     * @return
     */
    public List<HashMap> findModuleListByParentId(String parent_id) {
    	HashMap map = new HashMap();
    	map.put("PARENT_ID", parent_id);
        return this.query("findModuleListByParentId", map);
    }

    public List findSecondLevelModuleListByLoginID(String MODULE_ID,
            String LOGIN_ID) {
        int NOT_ZERO_COUNT = matchChar(MODULE_ID, '0');
        HashMap map = new HashMap();
        char[] chs = MODULE_ID.toCharArray();
        // 统计0位数
        int FIRST_MODULE_ID = 0;
        for (int j = 0; j < chs.length; j++) {
            if ('0' == chs[j]) {
                FIRST_MODULE_ID++;
            }
            // 如果碰到有不为0的位数或者只有偶数位为0的清为零
            if (chs[j] != '0'
                    || (j > 0 && j % 2 != 0 && chs[j - 1] != '0' && '0' == chs[j])) {
                FIRST_MODULE_ID = 0;
            }
        }
        String FIRST_ZERO_MODULE = "";
        for (int m = 0; m < FIRST_MODULE_ID; m++) {
            FIRST_ZERO_MODULE += "0";
        }
        map.put("FIRST_MODULE_ID", MODULE_ID.substring(0,
                10 - FIRST_MODULE_ID));
        map.put("NOT_ZERO_COUNT", String.valueOf(NOT_ZERO_COUNT));
        map.put("FIRST_ZERO_MODULE", FIRST_ZERO_MODULE);
        map.put("LOGIN_ID", LOGIN_ID);

        return this.query(
                    "findSecondLevelModuleListByLoginID", map);
    }

    public int matchChar(String s, char ch) {
        char[] chs = s.toCharArray();
        int i = 0;
        for (int j = 0; j < chs.length; j++) {
            if (ch == chs[j]) {
                i++;
            }
            if (chs[j] != '0'
                    || (j > 0 && j % 2 != 0 && chs[j - 1] != ch && ch == chs[j])) {
                i = 0;
            }
        }

        return 10 - i;
    }

    public List getAllModuleForReport(String loginId, String pre_super,
            int pre_super_len) {
        String mysql = "SELECT DISTINCT a.module_id, b.module_name, b.module_path"
                + " FROM tb_sys_role_module a, tb_sys_module b"
                + " WHERE role_id IN (SELECT role_id FROM tb_sys_user_role WHERE login_id = '"
                + loginId
                + "') AND a.module_id = b.module_id and b.module_enable='1'"
                + " AND a.module_id LIKE '" + pre_super + "%'";
        if (pre_super != null && pre_super.length() == 2) {
            mysql += " AND instr(a.module_id,'00') in (4,5,6,7)";
        } else if (pre_super != null && pre_super.length() == 4) {
            mysql += " AND instr(a.module_id,'00') in (5,7)";
        }
        return this.query("getAllModuleForReport", mysql);

    }

    public List checkPAGE_ELEMENT(String PAGE_ELEMENT) {
        String mySql = "";
        mySql = " select PAGE_ELEMENT from tb_sys_module where PAGE_ELEMENT = '"
                + PAGE_ELEMENT + "'";

        return this.query("checkPAGE_ELEMENT", mySql);
    }

	@Override
	public List<TblSYS_MODULE> findAllModuleForRoleConfig(String systemId) {
		return this.query("findAllModuleForRoleConfig",systemId);
	}

	@Override
	public String getModuleDirectory(String module_id) {
		Object obj = this.querySingle("getModuleDirectory",module_id);
		if(obj!=null)
			return obj.toString();
		return null;
	}

    @Override
    public List<Map> selectModuleForTokenInput(String input,String num,String user) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("login_id",user);
        map.put("input",input);
        map.put("num",Integer.parseInt(num));

        return this.query("selectModuleForTokenInput",map);
    }

    @Override
	public List<HashMap> findAllModuleListByLoginIDAndParentId(String LOGIN_ID,
			String parent_id) {
		HashMap map = new HashMap();
        map.put("LOGIN_ID", LOGIN_ID);
        map.put("PARENT_ID", parent_id);
        return this.query("findAllModuleListByLoginIDAndParentId", map);
	}

	@Override
	public List<HashMap> findAllModuleListByParentId(String parent_id) {
		HashMap map = new HashMap();
    	map.put("PARENT_ID", parent_id);
        return this.query("findAllModuleListByParentId", map);
	}

    @Override
    public TblSYS_MODULE getModuleByPath(String path) {
        return (TblSYS_MODULE)this.querySingle("getSysModuleByPath",path);
    }
}
